//
//  UIImage+Scale.swift
//  图片选择器
//
//  Created by Raymond on 16/1/2.
//  Copyright © 2016年 liu. All rights reserved.
//

import UIKit
extension UIImage {
    func scaleImage() -> UIImage {
        // 设置图片缩小大小
        let newWidth: CGFloat = 300
        if self.size.width < newWidth {
            return self
        }
        let newHeight = newWidth * size.height / size.width
        
        let newSize = CGSize(width: newWidth, height: newHeight)
        // 开启绘图上下文绘图
        UIGraphicsBeginImageContext(newSize)
        // 将图片绘制到上下文中
        self.drawInRect(CGRect(origin: CGPointZero, size: newSize))
        // 获取绘制好得图片
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        // 关闭上下文
        UIGraphicsEndImageContext()
        
        return newImage
    }
}
